package com.wlyuan.open.domain.source.dispatcher.conversion;

import com.wlyuan.core.exception.AssertUtils;
import com.wlyuan.core.exception.ValueError;
import com.wlyuan.open.dto.tenant.OpenTenantRelationSyncDTO;
import com.wlyuan.open.utils.LongUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;

/**
 * @author lijing
 * @description
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class OpenDispatcherTenantRelationConversion implements OpenDispatcherConversion<OpenTenantRelationSyncDTO> {
    private final BaseOpenDispatcherConversion baseOpenDispatcherConversion;

    @Override
    public void convert(OpenTenantRelationSyncDTO source) {
        //转换租户
        var id = source.getId();
        if (LongUtils.notNullLong(id)) {
            var tenantId = baseOpenDispatcherConversion.tenant(id);
            AssertUtils.assertNotNull(tenantId, new ValueError("OpenTenantRelationSyncDTO 被邀请者不存在:" + id), source);
            source.setTenantId(tenantId);
            source.setId(tenantId);
        }

        //转换邀请者租户 和 邀请者租户ID （全量同步时）
        var inviterTenantId = source.getInviterTenantId();
        if (LongUtils.notNullLong(inviterTenantId)) {
            var tenantId = baseOpenDispatcherConversion.tenant(inviterTenantId);
            AssertUtils.assertNotNull(tenantId, new ValueError("OpenTenantRelationSyncDTO 邀请租户不存在:" + inviterTenantId), source);
            source.setInviterTenantId(tenantId);
        }
        var inviterUserId = source.getInviterUserId();
        if (LongUtils.notNullLong(inviterUserId)) {
            var userId = baseOpenDispatcherConversion.tenant(inviterUserId);
            AssertUtils.assertNotNull(userId, new ValueError("OpenTenantRelationSyncDTO 邀请用户不存在:" + inviterUserId), source);
            source.setInviterUserId(userId);
        }

    }
}
